﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace ChipSharp
{
    class Rail : Unmovable
    {
        byte mOrientation = 0;

        // bottom   right  top  left
        // 
        // high bit          low bit

        public Rail(int x, int y, byte orientation)
            : base(x, y)
        {
            mOrientation = orientation;
        }

        public override bool Block(bool inside, World.Direction direction)
        {
            switch (direction)
            {
                case World.Direction.kDown:
                    return (inside && ((mOrientation & 0x8) != 0) || !inside && ((mOrientation & 0x2) != 0));
                case World.Direction.kLeft:
                    return (inside && ((mOrientation & 0x1) != 0) || !inside && ((mOrientation & 0x4) != 0));
                case World.Direction.kRight:
                    return (inside && ((mOrientation & 0x4) != 0) || !inside && ((mOrientation & 0x1) != 0));
                case World.Direction.kUp:
                    return (inside && ((mOrientation & 0x2) != 0) || !inside && ((mOrientation & 0x8) != 0));
            }

            return false;
        }

        public override void Draw(System.Drawing.Graphics graph, System.Drawing.Rectangle rect)
        {
            Pen pen = new Pen(Color.Black);
            SolidBrush brush = new SolidBrush(Color.Gray);

            graph.DrawRectangle(pen, rect);
            Rectangle fillRect = new Rectangle(rect.X + 1, rect.Y + 1,
                rect.X + rect.Width - 1, rect.Y + rect.Height - 1);
            graph.FillRectangle(brush, fillRect);


            SolidBrush railBrush = new SolidBrush(Color.Black);
            Rectangle railRect;

            if ((mOrientation & (int)World.Direction.kDown) != 0)
            {
                railRect = new Rectangle(rect.X + 1, 
                    rect.Y + rect.Height - 3,
                    rect.Width - 2,
                    2);
                graph.FillRectangle(railBrush, railRect);
            }
            if ((mOrientation & (int)World.Direction.kLeft) != 0)
            {
                railRect = new Rectangle(rect.X + 1,
                    rect.Y + 1,
                    2,
                    rect.Height - 2);
                graph.FillRectangle(railBrush, railRect);
            }
            if ((mOrientation & (int)World.Direction.kRight) != 0)
            {
                railRect = new Rectangle(rect.X + rect.Width - 3,
                    rect.Y + 1,
                    2,
                    rect.Height - 2);
                graph.FillRectangle(railBrush, railRect);
            }
            if ((mOrientation & (int)World.Direction.kUp) != 0)
            {
                railRect = new Rectangle(rect.X + 1,
                    rect.Y + 1,
                    rect.Width - 2,
                    2);
                graph.FillRectangle(railBrush, railRect);
            }

            base.Draw(graph, rect);
        }
    }
}
